package com.foodcommunity.controller;

import com.foodcommunity.common.Result;
import com.foodcommunity.dto.FoodPostDTO;
import com.foodcommunity.entity.FoodPost;
import com.foodcommunity.entity.User;
import com.foodcommunity.service.FoodPostService;
import com.foodcommunity.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.time.LocalDateTime;

/**
 * 管理员API控制器
 * 处理管理员相关API请求，包括用户管理
 */
@RestController
@RequestMapping("/api/admin")
public class AdminApiController {

    @Autowired
    private UserService userService;
    
    @Autowired
    private FoodPostService foodPostService;
    
    /**
     * 获取用户详情
     */
    @GetMapping("/user/{id}")
    public Result<User> getUserDetail(@PathVariable Long id, HttpSession session) {
        // 检查是否登录且是管理员
        User currentUser = (User) session.getAttribute("user");
        if (currentUser == null || !"admin".equals(currentUser.getRole())) {
            return Result.error("无权限操作");
        }
        
        return userService.getUserInfo(id);
    }
    
    /**
     * 更新用户信息
     */
    @PutMapping("/user/{id}")
    public Result<String> updateUser(@PathVariable Long id, @RequestBody User user, HttpSession session) {
        // 检查是否登录且是管理员
        User currentUser = (User) session.getAttribute("user");
        if (currentUser == null || !"admin".equals(currentUser.getRole())) {
            return Result.error("无权限操作");
        }
        
        // 设置ID和更新时间
        user.setId(id);
        user.setUpdateTime(LocalDateTime.now());
        
        boolean success = userService.updateUser(user);
        if (success) {
            return Result.success("更新成功");
        } else {
            return Result.error("更新失败");
        }
    }
    
    /**
     * 删除用户
     */
    @DeleteMapping("/user/{id}")
    public Result<String> deleteUser(@PathVariable Long id, HttpSession session) {
        // 检查是否登录且是管理员
        User currentUser = (User) session.getAttribute("user");
        if (currentUser == null || !"admin".equals(currentUser.getRole())) {
            return Result.error("无权限操作");
        }
        
        // 调用服务删除用户
        boolean success = userService.deleteUser(id);
        if (success) {
            return Result.success("删除成功");
        } else {
            return Result.error("删除失败，可能是用户不存在或是管理员账号");
        }
    }
    
    /**
     * 获取美食日记详情
     */
    @GetMapping("/post/{id}")
    public Result<FoodPostDTO> getPostDetail(@PathVariable Long id, HttpSession session) {
        // 检查是否登录且是管理员
        User currentUser = (User) session.getAttribute("user");
        if (currentUser == null || !"admin".equals(currentUser.getRole())) {
            return Result.error("无权限操作");
        }
        
        // 获取美食日记详情
        Result<FoodPost> postResult = foodPostService.getPostDetail(id);
        if (postResult.getCode() != 200) {
            return Result.error(postResult.getMsg());
        }
        
        // 转换为DTO对象，包含更多信息
        FoodPostDTO postDTO = new FoodPostDTO();
        FoodPost post = postResult.getData();
        // 这里需要根据实际情况设置DTO的属性
        // 通常会包含作者名称、分类名称等信息
        
        return Result.success(postDTO);
    }
    
    /**
     * 更新美食日记
     */
    @PutMapping("/post/{id}")
    public Result<String> updatePost(@PathVariable Long id, @RequestBody FoodPost post, HttpSession session) {
        // 检查是否登录且是管理员
        User currentUser = (User) session.getAttribute("user");
        if (currentUser == null || !"admin".equals(currentUser.getRole())) {
            return Result.error("无权限操作");
        }
        
        // 设置ID
        post.setId(id);
        
        // 调用服务更新美食日记
        Result<FoodPost> result = foodPostService.updatePost(post);
        if (result.getCode() == 200) {
            return Result.success("更新成功");
        } else {
            return Result.error(result.getMsg());
        }
    }
    
    /**
     * 删除美食日记
     */
    @DeleteMapping("/post/{id}")
    public Result<String> deletePost(@PathVariable Long id, HttpSession session) {
        // 检查是否登录且是管理员
        User currentUser = (User) session.getAttribute("user");
        if (currentUser == null || !"admin".equals(currentUser.getRole())) {
            return Result.error("无权限操作");
        }
        
        // 调用服务删除美食日记
        // 管理员可以删除任何人的美食日记，所以这里传入null作为userId
        Result<Void> result = foodPostService.deletePost(id, null);
        if (result.getCode() == 200) {
            return Result.success("删除成功");
        } else {
            return Result.error(result.getMsg());
        }
    }
}